package dk.oldommer.bjw.pages;

import org.apache.log4j.Logger;
import org.apache.tapestry.IPage;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.html.BasePage;

import dk.oldommer.bjw.model.user.*;

public abstract class AddUser extends BasePage implements PageBeginRenderListener {

	@InjectPage("ShowUser")
	public abstract ShowUser getShowUserPage();

	public abstract void setUser(UserValues userValues);

	public abstract UserValues getUser();
	
	Logger logger = Logger.getLogger(AddUser.class);


	public IPage doSubmit() {
		logger.debug("doSubmit()");
		try {
			UserFacade userFacade = new UserFacadeImpl();
			User user = userFacade.create(getUser());
			setUser(user);
			ShowUser showUser = getShowUserPage();
			showUser.setUser(getUser());
			return showUser;
		}

		catch (FacadeException e) {
			logger.error("Failed creating user", e);
			return null;
		}

	}

	@Override
	public void pageBeginRender(PageEvent event) {
		System.out.println("*****************");

		if (getUser() == null) {
			UserValues userValues = new User();
			setUser(userValues);
		}

	}

}
